import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import allTestDetail from "@/views/testDetail/allTestDetail.vue";
import Login from "@/views/Login.vue";

import Register from "@/views/Register";
import Home from "@/views/Home";
import mainBody from "@/components/mainBody";
import BackgroundManage from "@/views/Background/BackgroundManage";
import RoleManage from "@/views/Background/roleAndMenu/RoleManage";
import MenuManage from "@/views/Background/PermissionsAndMenu/MenuManage";
import PersonalCenter from "@/views/personalCenter/PersonalCenter.vue";
import ManagerMain from "@/components/ManagerMain";
import myCollect from "@/views/personalCenter/myCollect.vue";
import Bread from "@/components/Bread.vue";



const routes = [

    {
        path: '/', name: "Home", component: Home,
        children: [
            {path: '/', name: "MainBody", component: mainBody},
            {path: '/MainBody', name: "MainBody", component: mainBody},
            {path: '/PersonalCenter', name: "PersonalCenter", component:PersonalCenter},
            {path: '/Bread', name: "Bread", component:Bread},

            {
                path: '/TestDetail', name: "TestDetail", component: () => import('../views/detail/TestDetail'), children: [
                    {path: '/', name: "TestContent", component: () => import('../views/detail/TestContent')},
                    {path: '/TestContent', name: "TestContent", component: () => import('../views/detail/TestContent')},
                    {path: '/TestCurse', name: "TestCurse", component: () => import('../views/detail/TestCurse')},
                    {path: '/TestAnswer', name: "TestAnswer", component: () => import('../views/detail/TestAnswer')},
                ]
            },
        ],

    },

    {path: '/Login', name: "Login", component: Login},
    {path: '/Register', name: "Register", component: Register},


    {
        path: '/BackgroundManage', name: "BackgroundManage", component: BackgroundManage,
        children: [

            {path: '/RoleManage', name: "RoleManage", component: RoleManage},
            {path: '/MenuManage', name: "MenuManage", component: MenuManage},
            {path: '/ManagerMain', name: "ManagerMain", component: ManagerMain},
            {path: '/allTestDetail', name: "allTestDetail", component: allTestDetail},
            {path: '/myCollect', name: "myCollect", component: myCollect},
        ]
    },
]


const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {
    if (to.path === "/" || to.path === "/Login" || to.path === "/Register" || to.path === "/MainBody" ||to.path === "/Bread"  ) {
        console.log(from.path,to.path)
        if (from.path==="/Bread"&&to.path!="/Bread"){
            sessionStorage.removeItem("parentType")
        }
        next();
        return;
    }


    let token = sessionStorage.getItem("token");
    if (!token) {
        next("/Login");
        return;
    }
    // next();
})


export default router
